<template>
  <div style="">
    <Header :left="true" leftText="返回" title="问题产品处置申请"> </Header>
    <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
      left-icon="description">基本信息</NoticeBar>
    <van-form :style="autoHeight" @submit="onSubmit">
      <van-field class="formstyle" required readonly v-model="form.POrderID" name="订单号" label="订单号" placeholder="自带" />
      <van-field class="formstyle" required readonly v-model="form.BatchID" name="问题批次号" label="问题批次号" placeholder="自带" />
      <van-field class="formstyle" required v-model="form.DeFectAmount" name="待处理数量" label="待处理数量" placeholder="手输" />
      <van-field class="formstyle" required readonly v-model="form.QualityAppearanceCheckId" name="外观检测单号" label="外观检测单号"
        placeholder="自动填充检验单号" />
      <van-field required readonly class="formstyle" name="picker" :value="valueTeamID" label="申请处置班组"
        placeholder="点击选择申请处置班组" @click="showPickerTeamID = true" />
      <van-popup v-model="showPickerTeamID" position="bottom">
        <van-picker show-toolbar :columns="columnsTeamID" @confirm="onConfirmTeamID" @cancel="showPickerTeamID = false">
          <template #option="option">
            <div style="display: flex; flex-direction: column; align-items: center">
              <div>{{ option.TeamName }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>

      <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
        left-icon="description">缺陷信息 - 严重缺陷</NoticeBar>
      <van-field readonly class="formstyle" name="picker" :value="showManageReasonID(ManageReasonName1)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />
      <van-field readonly class="formstyle" name="picker" :value="showManageReasonID(ManageReasonName2)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />
      <van-field readonly class="formstyle" name="picker" :value="showManageReasonID(ManageReasonName3)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />

      <NoticeBar color="#1989fa" background="#ecf9ff" style="margin-top: 5px; font-size: 15px; font-weight: bold"
        left-icon="description">缺陷信息 - 一般缺陷</NoticeBar>
      <van-field readonly class="formstyle" name="picker" :value="showQProblemID(QProblemName1)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />
      <van-field readonly class="formstyle" name="picker" :value="showQProblemID(QProblemName2)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />
      <van-field readonly class="formstyle" name="picker" :value="showQProblemID(QProblemName3)" label="缺陷原因"
        placeholder="点击选择缺陷原因" />

      <van-field class="formstyle" required v-model="form.DefectMemo" name="需注明的缺陷描述" label="需注明的缺陷描述"
        placeholder="输入缺陷描述" />
      <van-field class="formstyle" required readonly :value="valueEmployeeName" name="发送品管领班" label="发送品管领班"
        placeholder="输入品管领班" @click="showPickerEmployee = true" />
      <van-popup v-model="showPickerEmployee" position="bottom">
        <van-picker show-toolbar :columns="columnsEmployee" @confirm="onConfirmEmployee"
          @cancel="showPickerEmployee = false">
          <template #option="option">
            <div style="display: flex; flex-direction: column; align-items: center">
              <div>{{ option.EmployeeName }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>
      <!-- 
      <div style="margin: 16px">
        <van-button color="#138087" round block type="info" native-type="submit" @click="submitsure">保存</van-button>
      </div> -->

      <van-row class="button" gutter="2">
        <van-col span="12">
          <van-button color="#138087" round block type="primary" native-type="submit" @click="submitsure1">跳过</van-button>
        </van-col>
        <van-col span="12">
          <van-button color="#138087" round block type="info" native-type="submit" @click="submitsure">保存</van-button>
        </van-col>
      </van-row>


    </van-form>
  </div>
</template>
  
<script>
import {
  Col,
  Row,
  Form,
  Cell,
  Icon,
  Field,
  NumberKeyboard,
  Image as VanImage,
  Picker,
  popup,
  SubmitBar,
  Button,
  Loading,
  Overlay,
  Dialog,
  Toast,
  NoticeBar,
  Tag,
  DatetimePicker,
  ImagePreview,
  CellGroup,
  Uploader,
  Cascader,
  Calendar,
  RadioGroup,
  Radio,
  Divider,
  Stepper,
  AddressList,
  Checkbox,
  CheckboxGroup,
} from "vant";

import "vue-easytable/libs/themes-base/index.css";
import { VTable } from "vue-easytable";
import { _httpClient as _httpClientApisie } from "@/libs/apisie";
import { _httpClient as _httpClientApi } from "@/libs/api";
import { getToken, getBaseApiFrefix } from "@/libs/auth";
import Header from "@/components/header.vue";
import { storage } from "@/libs/utils";

Vue.use(Form);
Vue.use(Field);
Vue.use(Overlay);
Vue.use(Loading);
Vue.use(Picker);
Vue.use(popup);
Vue.use(SubmitBar);
Vue.use(VanImage);
Vue.use(DatetimePicker);
Vue.use(Cascader);
Vue.use(Calendar);
Vue.use(Cell);
Vue.use(RadioGroup);
Vue.use(Radio);
Vue.use(Divider);
Vue.use(Stepper);
Vue.use(Button);
Vue.use(AddressList);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Col);
Vue.use(Row);

export default {
  name: "qCcheckApplication",
  components: {
    Form,
    Row,
    Col,
    Button,
    Header,
    Form,
    Cell,
    Icon,
    Field,
    NumberKeyboard,
    Dialog,
    Toast,
    NoticeBar,
    Row,
    Tag,
    popup,
    DatetimePicker,
    ImagePreview,
    CellGroup,
    Uploader,
    Col,
    Calendar,
    Cascader,
    Picker,
    RadioGroup,
    Radio,
    Divider,
    Stepper,
    VTable,
    AddressList,
    Checkbox,
    CheckboxGroup,
  },
  data() {
    return {
      minDate: new Date(2023, 1, 1),
      maxDate: new Date(2025, 10, 1),
      currentdate: new Date(),
      form: {},
      user: [],
      valueEmployeeName: "",
      columnsEmployee: [],
      showPickerEmployee: false,
      valueTeamID: "",
      columnsTeamID: [],
      showPickerTeamID: false,
      QProblemName1: "",
      QProblemName2: "",
      QProblemName3: "",
      ManageReasonName1: "",
      ManageReasonName2: "",
      ManageReasonName3: "",
      QProblemIDList: ["空", "空", "空"],
      columnsQProblem: [],
      columnsManageReason: [],
      ManageReasonIDList: ["空", "空", "空"],
    };
  },
  created: function () {
    this.user = JSON.parse(storage.get("user"));
    this.getEmployeeIDByPositionIDGW11();
    this.getQualityProblems();
    this.getManageReasonID();
    if (typeof api.pageParam.item != "undefined") {

      this.form = api.pageParam.item;
      this.form.DefectMemo = this.form.Memo;
      this.form.QualityAppearanceCheckId = this.form.Id;
      this.QProblemIDList = JSON.parse(this.form.QProblemID);
      if (this.QProblemIDList.length == 3) {
        this.QProblemName1 = this.QProblemIDList[0];
        this.QProblemName2 = this.QProblemIDList[1];
        this.QProblemName3 = this.QProblemIDList[2];
      } else if (this.QProblemIDList.length == 0) {
        this.QProblemName1 = "无异常";
        this.QProblemName2 = "无异常";
        this.QProblemName3 = "无异常";
      } else if (this.QProblemIDList.length == 1) {
        this.QProblemName1 = this.QProblemIDList[0];
      } else if (this.QProblemIDList.length == 2) {
        this.QProblemName1 = this.QProblemIDList[0];
        this.QProblemName2 = this.QProblemIDList[1];
      }

      this.ManageReasonIDList = JSON.parse(this.form.ManageReasonID);
      if (this.ManageReasonIDList.length == 3) {
        this.ManageReasonName1 = this.ManageReasonIDList[0];
        this.ManageReasonName2 = this.ManageReasonIDList[1];
        this.ManageReasonName3 = this.ManageReasonIDList[2];
      } else if (this.ManageReasonIDList.length == 0) {
        this.ManageReasonName1 = "无异常";
        this.ManageReasonName2 = "无异常";
        this.ManageReasonName3 = "无异常";
      } else if (this.ManageReasonIDList.length == 1) {
        this.ManageReasonName1 = this.ManageReasonIDList[0];
      } else if (this.ManageReasonIDList.length == 2) {
        this.ManageReasonName1 = this.ManageReasonIDList[0];
        this.ManageReasonName2 = this.ManageReasonIDList[1];
      }

      this.getTeamID();


      this.form.QProblemID = JSON.stringify(this.QProblemIDList.concat(this.ManageReasonIDList).join(";"))
    }
  },
  mounted() { },
  methods: {
    getNowTimeplus() {
      let dateTime
      let yy = new Date().getFullYear()
      let mm = new Date().getMonth() + 1
      let dd = new Date().getDate()
      let hh = new Date().getHours()
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes()
        :
        new Date().getMinutes()
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds()
        :
        new Date().getSeconds()
      dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
      return dateTime
    },
    async sendmessage() {
      let _this = this;

      let eventDescription = "";
      let eventDatetime = _this.getNowTimeplus();
      let eventTypeID = "N3";
      let eventID = "";
      let messageContent = "";
      let msgContentTypeID = "";
      let pLineID = _this.form.PLineID;
      let machineID = _this.form.PLineID;
      let pShiftID = "";
      let teamID = "";
      let pOrderID = _this.form.POrderID;
      let productionBarcode = "";
      let materialBarcode = "";
      let relatedEmployee = "";
      let messageStatus = "";
      let processBatchID = "";
      let beginDatetime = _this.getNowTimeplus();
      let sendFinished = false;
      let closed = false;

      msgContentTypeID = "EV001";
      eventID = "DF001";
      eventDescription = "问题产品处置申请单于:" + _this.getNowTimeplus() + "被创建，请品管领班进行处理！";
      messageContent = "生产订单" + _this.form.POrderID + "在生产线:" + _this.form.PLineID + "上生产时发生质量问题，质量原因是:" + _this.form.DefectMemo + ".请按照流程进行问题产品异常申请评估！";

      let bodysss = {
        eventDescription: eventDescription,
        eventDatetime: eventDatetime,
        eventTypeID: eventTypeID,
        eventID: eventID,
        messageContent: messageContent,
        msgContentTypeID: msgContentTypeID,
        factoryID: "TC00",
        pLineID: pLineID,
        machineID: machineID,
        pShiftID: pShiftID,
        teamID: teamID,
        pOrderID: pOrderID,
        productionBarcode: productionBarcode,
        materialBarcode: materialBarcode,
        relatedEmployee: relatedEmployee,
        messageStatus: messageStatus,
        processBatchID: processBatchID,
        beginDatetime: beginDatetime,
        sendFinished: sendFinished,
        closed: closed
      };
      try {
        let res = await _httpClientApisie.post(
          _httpClientApisie.getApiPrefix() + "/RealtimeEventMessage/SaveEntity",
          {
            body: bodysss
          }
        );
        //alert(JSON.stringify(res));
        if (res.Message == null) {
          Toast.clear();
          Toast.success("发送成功");

          setTimeout(function () {
            _this.$ac.closeWin();
          }, 2000);

          // api.sendEvent({
          //   name: "reloadCK01",
          // });
          // setTimeout(function () {
          //   _this.$ac.closeWin();
          // }, 2000);
          // setTimeout(function () {
          //   _this.$ac.closeWin();
          // }, 2000);
          // api.openWin({
          //   name: "quality-qCcheckApplication",
          //   url: "./quality-qCcheckApplication.html",
          //   reload: true,
          //   pageParam: {
          //     item: _this.item2,
          //     // item2: _this.item2,
          //   },
          // });
        } else {
          Toast.clear();
          Toast.fail("发送失败");
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    showQProblemID(QProblemID) {
      let _this = this;
      var QProblemName = QProblemID;
      _this.columnsQProblem.forEach((item) => {
        if (item.QProblemID === QProblemID) {
          QProblemName = item.QProblemName;
        }
      });
      return QProblemName;
    },
    async getQualityProblems() {
      let _this = this;
      try {
        let res = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/QualityProblems/GetList"
        );
        if (res.HttpCode === "200") {
          let qProblemNameMap = res.Data.map((item) => ({
            QProblemID: item.QProblemID,
            QProblemName: item.QProblemName,
          }));
          _this.columnsQProblem = qProblemNameMap;
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },

    showManageReasonID(ManageReasonID) {
      let _this = this;
      var ManageReasonName = ManageReasonID;
      _this.columnsManageReason.forEach((item) => {
        if (item.ManageReasonID === ManageReasonID) {
          ManageReasonName = item.ManageReasonName;
        }
      });
      return ManageReasonName;
    },
    async getManageReasonID() {
      let _this = this;
      try {
        let res = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/QualityManageReason/GetList"
        );
        if (res.HttpCode === "200") {
          let ManageReasonNameMap = res.Data.map((item) => ({
            ManageReasonID: item.ManageReasonID,
            ManageReasonName: item.ManageReasonName,
          }));
          _this.columnsManageReason = ManageReasonNameMap;
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    onConfirmTeamID(value, index) {
      this.form.TeamID = this.columnsTeamID[index].TeamID;
      this.valueTeamID = this.columnsTeamID[index].TeamName;
      this.showPickerTeamID = false;
    },
    //获取班组信息
    async getTeamID() {
      let _this = this;
      try {
        let res = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/Team/GetList?FactoryID=TC00"
        );
        if (res.HttpCode === "200") {
          let teamMap = res.Data.map((item) => ({
            TeamID: item.TeamID,
            TeamName: item.TeamName,
          }));
          _this.columnsTeamID = teamMap;


          for (let i = 0; i < _this.columnsTeamID.length; i++) {
            if (_this.form.TeamID == _this.columnsTeamID[i].TeamID) {
              _this.valueTeamID = _this.columnsTeamID[i].TeamName
              break
            }
          }
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    onConfirmEmployee(value, index) {
      this.form.QCLeaderUserID = this.columnsEmployee[index].EmployeeID;
      this.valueEmployeeName = this.columnsEmployee[index].EmployeeName;
      this.showPickerEmployee = false;
    },
    //获取员工列表
    async getEmployeeIDByPositionIDGW11() {
      let _this = this;
      try {
        let res = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/Employee/GetList?PositionID=GW11"
        );
        if (res.HttpCode === "200") {
          //Toast.fail(JSON.stringify("成功进入"));
          let temp = res.Data.map((item) => ({
            EmployeeID: item.EmployeeID,
            EmployeeName: item.EmployeeName,
          }));
          temp.forEach((item) => {
            _this.columnsEmployee.push(item);
          });
        }

        let res2 = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/Employee/GetList?PositionID=GW32"
        );
        if (res2.HttpCode === "200") {
          //Toast.fail(JSON.stringify("成功进入"));
          let temp = res2.Data.map((item) => ({
            EmployeeID: item.EmployeeID,
            EmployeeName: item.EmployeeName,
          }));
          temp.forEach((item) => {
            _this.columnsEmployee.push(item);
          });
        }

        let res3 = await _httpClientApisie.get(
          _httpClientApisie.getApiPrefix() + "/Employee/GetList?PositionID=GW33"
        );
        if (res3.HttpCode === "200") {
          //Toast.fail(JSON.stringify("成功进入"));
          let temp = res3.Data.map((item) => ({
            EmployeeID: item.EmployeeID,
            EmployeeName: item.EmployeeName,
          }));
          temp.forEach((item) => {
            _this.columnsEmployee.push(item);
          });
        }

      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
    async submitsure1() {
      Toast.success("跳过操作");
      let _this = this;
      setTimeout(function () {
        _this.$ac.closeWin();
      }, 2000);

    },
    async submitsure() {
      let _this = this;
      Toast.loading({
        mask: true,
        forbidClick: true,
        duration: 0,
        message: "请稍等...",
      });

      var qy = _this.QProblemName1 + ";" + _this.QProblemName2 + ";" + _this.QProblemName3 + ";" + _this.ManageReasonName1 + ";" + _this.ManageReasonName2 + ";" + _this.ManageReasonName3
      let bodyss = {
        factoryID: "TC00",
        PLineID: _this.form.PLineID,
        POrderID: _this.form.POrderID,
        MaterialID: _this.form.MaterialID,
        MaterialName: _this.form.MaterialName,
        BatchID: _this.form.BatchID,
        ProductionDate: _this.form.ProductionDate,
        PShiftID: _this.form.PShiftID,
        TeamID: _this.form.TeamID,
        DeFectAmount: _this.form.DeFectAmount,
        MUnitID: "PC",
        QCLeaderAdviceDealType: "DT01",
        QCManagerAdviceDealType: "DT01",
        PrdManagerAdviceDealType: "DT01",
        FactoryManagerDealType: "DT01",
        AdviceDealType: "DT01",
        AppearanceCheckRecord: _this.form.QualityAppearanceCheckId,
        QProblemID: qy,
        DefectMemo: _this.form.DefectMemo,
        QCLeaderUserID: _this.form.QCLeaderUserID,
        Creator: _this.user.userName,
        CreateDatetime: new Date(),
      };
      //alert(JSON.stringify(bodyss));
      try {
        let res = await _httpClientApisie.post(
          _httpClientApisie.getApiPrefix() + "/QualityDefectReview/SaveEntity",
          {
            body: bodyss,
          }
        );
        //alert(JSON.stringify(res));
        if (res.Message == null) {
          Toast.clear();
          Toast.success("保存成功");
          _this.sendmessage();
        } else {
          Toast.clear();
          Toast.fail("保存失败" + res.Message);
        }
      } catch (e) {
        Toast.fail(JSON.stringify(e));
      }
    },
  },
};
</script>
  
<style></style>
  